package com.nirvana.convert;

import org.apache.commons.lang3.StringUtils;

public class CamelCaseConverter {
    public static String toCamelCase(String input, boolean lower) {
        if (StringUtils.isBlank(input)) {
            return input;
        }

        // 使用正则表达式替换所有非字母数字字符为一个空格
        String[] words = input.replaceAll("[^a-zA-Z0-9]", " ").trim().split("\\s+");

        StringBuilder result = new StringBuilder();

        char firstChar = words[0].charAt(0);
        if (lower) {
            firstChar = Character.toLowerCase(firstChar);
        } else {
            firstChar = Character.toUpperCase(firstChar);
        }
        result.append(firstChar);
        result.append(words[0].substring(1));
        for (int i = 1; i < words.length; i++) {
            result.append(words[i].substring(0, 1).toUpperCase());
            result.append(words[i].substring(1));
        }

        return result.toString();
    }

    public static String camelToSnakeCase(String camelCase) {
        // 使用正则表达式匹配大写字母，除了字符串开头的第一个大写字母
        String regex = "(?<!^)(?=[A-Z])";
        // 将匹配到的大写字母替换为下划线加该大写字母
        return camelCase.replaceAll("`", "").replaceAll(regex, "_$0").toLowerCase();
    }

    public static void main(String[] args) {
        String input = "hello_world_example";
        String output = toCamelCase(input, true);
        System.out.println(output); // 输出: helloWorldExample
    }
}
